//////////////////////////////////////////////////////////////////////////
//
// Vector Graph Core2D
//
// Module:
//        DrawLib2D.cs
//
// Description:
//        Implementation of drawing graph elements using GDIPlus
//
// Author:
//        Created on 2008.04.14 by Destrayer
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace VectorGraph.Core2D
{
    class GDIPlusDrawLib2D
    {
        private Graphics g;

        public GDIPlusDrawLib2D(Graphics g)
        {
            this.g = g;
        }

        public void DrawPoint(Point2d pt, ElementTrait2D trait)
        {
            RectangleF r = new RectangleF((float)pt.X - trait.Width, (float)pt.Y - trait.Width, 2 * trait.Width, 2 * trait.Width);
            this.g.FillEllipse(trait.GetDrawBrush(), r);
        }

        public void DrawRectangle(Rectangle2d rectangle, ElementTrait2D trait)
        {
            Point2d pt;
            pt.X = rectangle.Right;
            pt.Y = rectangle.Bottom;
            this.DrawSegment(rectangle.LeftBottom, pt, trait);
            this.DrawSegment(pt, rectangle.RightTop, trait);
            pt.X = rectangle.Left;
            pt.Y = rectangle.Top;
            this.DrawSegment(rectangle.RightTop, pt, trait);
            this.DrawSegment(pt, rectangle.LeftBottom, trait);
        }

        public void DrawBeeline(Beeline2d beeline, ElementTrait2D trait)
        {
            // need bounding rectangle
        }

        public void DrawSegment(Point2d pt1, Point2d pt2, ElementTrait2D trait)
        {
            this.g.DrawLine(trait.GetDrawPen(), (float)pt1.X, (float)pt1.Y, (float)pt2.X, (float)pt2.Y);
        }

        public void DrawSegment(Segment2d seg, ElementTrait2D trait)
        {
            this.DrawSegment(seg.StartPt, seg.EndPt, trait);
        }

        public void DrawCircle(Circle2d circle, ElementTrait2D trait)
        {
            RectangleF r = new RectangleF(
                (float)(circle.CenterPt.X - circle.Radius),
                (float)(circle.CenterPt.Y - circle.Radius),
                (float)circle.Radius * 2,
                (float)circle.Radius * 2);
            this.g.DrawEllipse(trait.GetDrawPen(), r);
        }

        public void DrawArc(Arc2d arc, ElementTrait2D trait)
        {
            float radius = (float)arc.StartVector.Length;
            RectangleF r = new RectangleF(
                (float)(arc.CenterPt.X - radius),
                (float)(arc.CenterPt.Y - radius),
                radius * 2,
                radius * 2);
            this.g.DrawArc(trait.GetDrawPen(), r, (float)arc.StartAngle, (float)arc.SweepAngle);
        }

        public void DrawPath(Path2d path, ElementTrait2D trait)
        {
            for (int i = 0; i < path.Count - 1; i++)
            {
                this.DrawSegment(path.Vertex(i), path.Vertex(i + 1), trait);
            }
        }

        public void DrawPolygon(Polygon2d polygon, ElementTrait2D trait)
        {
            for (int i = 0; i < polygon.Count - 1; i++)
            {
                this.DrawSegment(polygon.Vertex(i), polygon.Vertex(i + 1), trait);
            }
            this.DrawSegment(polygon.Vertex(polygon.Count - 1), polygon.Vertex(0), trait);
        }
    }
}
